<?php
/**
 * Class Book for sewiki System
 * 
 * Define Book's attributes and operator
 * This system is devolop from CodeIgniter Framework
 * Welcome to visit http://codeigniter.com/
 * 
 * @package  CodeIgniter
 * @category model/book
 * @author   Oneplus <oneplus.lau@gmail.com>
 * @date     2011-05-27
 */
class Book extends CI_Model {
	function __construct() {
		parent::__construct();
		$this->load->database();
	}
	/**
	 * Create a book with book infomation in
	 * data
	 *
	 * @access public
	 * @param  data
	 * @return int, created book's id
	 */
	public function createBook($data) {
		$this->db->insert('books', $data);
		return $this->db->insert_id();
	}
	/**
	 * Modify a book with book information in
	 * data
	 *
	 * @access public
	 * @param  id, int
	 * @param  data, array
	 * @return void
	 */
	public function modifyBook($bid, $data) {
		$this->db->where('bid', $bid);
		$this->db->update('books', $data);
	}
	/**
	 * Get a book's info with book id
	 *
	 * @access public
	 * @param  bid, int
	 * @return array
	 */
	public function getBook($bid) {
		$book  = $this->db->get_where('books', array('bid' => $bid));
		$book  = $book->row();
		$admin = $this->db->get_where('users', array('uid' => $book->uid));
		$admin = $admin->row();
		return array(
			'bid'         => $bid,
			'title'       => $book->title,
			'description' => $book->description,
			'auid'        => $book->uid,
			'create_time' => $book->create_time,
			'admin_name'  => $admin->username);
	}
}
?>